clear all
set more off
capture log close
log using "$output/tables4_5_A1_A2.log", replace

version 16	

use "$data/acsmergeddata.dta", clear

gen poorcutoff=12060+4180*married+4180*(newnumkids17)
tab poorcutoff,m

drop faminc	famincctc21 famincctc21nother famincctc21notspouse			/* using official poverty line here, not spm as elsewhere */
gen faminc=ftotinc	
gen famincog=faminc
gen famincctc21=faminc+newnumkids17*3000+600*nchlt5
gen famincctc21nother=famincctc21-incwage
gen famincctc21notspouse=famincctc21-(totincwage-incwage)	/* if spouse stops working */
gen famincctc21nothernotspouse=famincctc21-(totincwage)	

foreach var in og {
	gen poor`var'=(faminc`var'<poorcutoff)
	}
drop poor
rename (poorog ) (poor )
drop poverty

gen ctc21=newnumkids17*3000+600*nchlt5

gen othernontaxableinc=totnontaxableinc-nontaxableinc

bysort married: sum spouseinctot spouseincwage

gen spouseotherinc=max(0,spouseinctot-spouseincwage)
gen otherfaminc=max(0,faminc-totinctot)

gen welfaressi=incwelfr +incsupp

gen tottaxablenother=tottaxableinc-incwage

replace ctc21=0 if tottaxableinc>400000

gen partner=(spousesex!=.)

replace spouseincwage=0 if spouseincwage==.
replace spouseinctot=0 if spouseinctot==.

gen notpoor1= (famincctc21-incwage<poorcutoff)
gen notpoor2= (famincctc21-ctc21- v25inc2- incss- welfaressi -incbus00 -incinvst -incretir- incother<poorcutoff)
gen notpoor3= (famincctc21-spouseincwage-spouseotherinc<poorcutoff)
gen notpoor4= (famincctc21-otherfaminc<poorcutoff)
gen notpoor5= (famincctc21nother-ctc21- v25inc2- incss- welfaressi -incbus00 -incinvst -incretir- incother<poorcutoff)
gen notpoor6= (famincctc21nother-ctc21- v25inc2- incss- welfaressi -incbus00 -incinvst -incretir- incother-spouseincwage-spouseotherinc<poorcutoff)
gen notpoor7= (famincctc21nother-ctc21- v25inc2- incss- welfaressi -incbus00 -incinvst -incretir- incother-otherfaminc<poorcutoff)
gen notpoor8= (famincctc21nother-ctc21- v25inc2- incss- welfaressi -incbus00 -incinvst -incretir- incother-spouseincwage-spouseotherinc-otherfaminc<poorcutoff)

*** Table 4
bysort married: sum notpoor* if poor==0 & sex==2 & samplejustwomen==1 & agi2017<80000 [aw=perwt]

gen mom= (poor==0 & sex==2 & samplejustwomen==1 & agi2017<80000)
tab mom
bysort married: egen totmoms=sum(perwt*mom)
bysort married: sum totmoms
bysort married: egen worktotmoms=sum(perwt*mom*(incwage>0))
bysort married: sum worktotmoms

*** Table A1
foreach j in 0 1{
	foreach i in 1 0{
	di "poor=`j' and married=`i'"
	sum incwage famincctc21 tottaxableinc tottaxablenother ctc21 v25inc2 incss welfaressi incbus00 incinvst incretir incother partner spouseincwage spouseotherinc otherfaminc newrtwchange if poor==0 & sex==2 & samplejustwomen==1 & calckids==2 & married==`i' & poorctc21nother==`j' [aw=perwt]
	}
}

*** Table 5
foreach j in 0 1{
	foreach i in 1 0{
	di "poor=`j' and married=`i'"
	sum incwage famincctc21 tottaxableinc tottaxablenother ctc21 v25inc2 incss welfaressi incbus00 incinvst incretir incother partner spouseincwage spouseotherinc otherfaminc newrtwchange if poor==0 & sex==2 & samplejustwomen==1 & calckids==2 & married==`i' & poorctc21nother==`j' & agi2017<80000 [aw=perwt]
	}
}

*** Table A2
foreach j in 0 1{
	foreach i in 1 0{
	di "poor=`j' and married=`i'"
	sum incwage famincctc21 tottaxableinc tottaxablenother ctc21 v25inc2 incss welfaressi incbus00 incinvst incretir incother partner spouseincwage spouseotherinc otherfaminc newrtwchange if poor==0 & sex==2 & samplejustwomen==1 & calckids==2 & married==`i' & poorctc21nother==`j' & newrtwchange>0 [aw=perwt]
	}
}

log close